package link.tengfei.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ling.tengfei.service.UserRegisterService;
import ling.tengfei.service.UserRegisterServiceImpl;
import link.tengfei.bean.AdminUser;
import link.tengfei.bean.GengralUser;

/**
 * Servlet implementation class UserRegisterServlet
 */
@WebServlet("/userregister")
public class UserRegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String select = request.getParameter("select");
		UserRegisterService registerService = new UserRegisterServiceImpl();
		if (select != null && select.equals("0")) {
			String username = request.getParameter("username");
			boolean flag = registerService.findByUsername(username);
			if (flag) {
				response.getWriter().print(1);
			} else {
				String userId = request.getParameter("user_id");
				flag = registerService.findByUserId(userId);
				if (flag) {
					response.getWriter().print(2);
				} else {
					String code = request.getParameter("code");
					if (code != null && code.equals(DrawImgServlet.randomNum())) {
						String name = request.getParameter("name");
						int sex = Integer.parseInt(request.getParameter("sex"));
						String age = request.getParameter("age");
						String password = request.getParameter("password");
						String email = request.getParameter("email");
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
						String adddate = df.format(new Date());
						GengralUser gengralUser = new GengralUser(name, sex, age, userId, username, password, email, 20,
								adddate, adddate, 0, "");
						int result = registerService.addGengralUser(gengralUser);
						if (result == 1) {
							System.out.println("添加成功");
							response.getWriter().print(3);
						} else {
							System.out.println("添加失败");
							response.getWriter().print(4);
						}
					} else {
						response.getWriter().print(5);
					}
				}
			}
		} else if (select != null && select.equals("1")) {
			String username = request.getParameter("username");
			boolean flag = registerService.findByUsername(username);
			if (flag) {
				response.getWriter().print(1);
			} else {
				int passid =Integer.parseInt( request.getParameter("user_id"));
				flag = registerService.findByPassId(passid);
				if (flag) {
					response.getWriter().print(2);
				} else {
					String code = request.getParameter("code");
					if (code != null && code.equals(DrawImgServlet.randomNum())) {
						String name = request.getParameter("name");
						int sex = Integer.parseInt(request.getParameter("sex"));
						String age = request.getParameter("age");
						String password = "000000";
						String email = request.getParameter("email");
						SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
						String adddate = df.format(new Date());
						AdminUser adminUser = new AdminUser(name, sex, age, passid, username, password, email,
								"", "",adddate, 1, 1);
						int result = registerService.addAdminUser(adminUser);
						if (result == 1) {
							System.out.println("添加成功");
							response.getWriter().print(3);
						} else {
							System.out.println("添加失败");
							response.getWriter().print(4);
						}
					} else {
						response.getWriter().print(5);
					}
				}
			}
		}
	}
}
